package com.ys.service.mall.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.dto.AddDto;
import com.ys.entity.mall.TmSignLog;
import com.ys.entity.sys.TbStaticConfig;
import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.entity.sys.TsRoleInfo;
import com.ys.entity.sys.TsUserInfo;
import com.ys.enume.finance.TcwUserPointRunningEnum;
import com.ys.mapper.mall.TmSignLogMapper;
import com.ys.service.finance.TcwAccountCountForUserService;
import com.ys.service.finance.TcwAccountCountForUserService.ChangePointDto;
import com.ys.service.finance.TcwAccountCountForUserService.ChangePointFrozenDto;
import com.ys.service.mall.TmSignLogService;
import com.ys.util.DateUtil;
import com.ys.vo.R;

@Service("tmSignLogService")
public class TmSignLogServiceImpl implements TmSignLogService {
	
	@Autowired
	private TmSignLogMapper tmSignLogMapper;
    
    @Autowired
    private TcwAccountCountForUserService tcwAccountCountForUserService;

	@Override
	@Transactional
	public R add(String userId) throws Exception {
		Date now = new Date();
		// 验证上一个签到时间必须在
		TmSignLog latest = new TmSignLog().setUserId(userId).setOrderby("A.id desc").queryFirst();
		if (latest != null && latest.getCreateTime().after(DateUtil.addSecond(now, -6 * 3600))) {
			return R.error("上一个签到时间必须在6个小时以外");
		}
		TsUserInfo tsUserInfo = new TsUserInfo().setUserId(userId).queryFirst();
		TsOrganizationInfo tsOrganizationInfo = new TsOrganizationInfo().setId(tsUserInfo.getOrgId()).queryFirst();
		TmSignLog entity = null;
		{
			String userName = tsUserInfo.getUserName();
			String deptId = tsUserInfo.getDeptId();
			String deptName = tsOrganizationInfo.getDeptName();
			String positionId = tsUserInfo.getPositionGuid();
			TsRoleInfo tsRoleInfo = new TsRoleInfo().setGuid(tsUserInfo.getPositionGuid()).queryFirst();
			String positionName = tsRoleInfo.getRoleName();
			String remark = null;
			entity = TmSignLog.tmSignLog(userId, userName, deptId, deptName, positionId, positionName, remark, 1);
		}
		tmSignLogMapper.insert(entity);
		
		BigDecimal baseNormal = new TbStaticConfig()
				.setKeyOne("point")
				.setKeyTwo("point-per-sign")
				.queryFirst().getAmount();
		{
        	String itemName = "学堂考核";
        	String itemNameSub = "日签到";
        	Integer type = TcwUserPointRunningEnum.Type.SIGN.value;
			Long relativeId = entity.getId();
			BigDecimal pointChange = baseNormal.negate();
			String remark = "用户" + userId + "签到";
			tcwAccountCountForUserService.changePoint_JIAO_WU_CHU(itemName, itemNameSub, type, relativeId, pointChange, remark);
		}
		{
        	String item = "学堂考核";
        	String itemSub = "日签到";
        	Integer type = TcwUserPointRunningEnum.Type.SIGN.value;
			Long relativeId = entity.getId();
            String userName = tsUserInfo.getUserName();
            BigDecimal pointChange = baseNormal;
            String remark = "签到获得学分";
			ChangePointDto changePointDto = new ChangePointDto(item, itemSub, type, relativeId, userId, userName, pointChange, remark);
			ChangePointFrozenDto changePointFrozenDto = null;
			tcwAccountCountForUserService.change(changePointDto, changePointFrozenDto);
		}
		
		Integer numContinue = 0;
		for (int i = 0; i < 7; i++) {
			String day = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1 * i));
			Integer numToday = new TmSignLog()
					.where("A.CreateTime >= '" + day + " 00:00:00'")
					.where("A.CreateTime <= '" + day + " 23:59:59'")
					.count();
			if (numToday <= 0) {
				break;
			} else {
				numContinue++;
			}
		}
		
		
		return R.ok(AddDto.closeAndReload());
	}
}
